<?php
namespace api\jeu\perso\caracteristique;
use api\jeu\perso as nsPerso;

if (!defined('GAME'))
{
	exit;
}

abstract class baseCaracteristique implements iCaracteristique{
	
	private $_observateur;
	protected $_events = array();
	private $_eventobserver;
	
	protected $_buffs;
	
	protected function __construct() 
	{
		$this->_observateur = new \SplObjectStorage();
		$this->_eventobserver = new EventObserverFactory();
		$this->_buffs = new \SplObjectStorage();	
	}
	
	final public function addBuff($buff)
	{
		$this->_buffs->attach($buff);
	}
	
	final public function removeBuff($buff)
	{
		$this->_buffs->detach($buff);
	}
	
	abstract public function refreshBuff();
	
	final protected function notify($event) 
	{
		foreach ($this->_observateur as $observateur) {
			try {
				if($observateur->getEvent() == $event) {
					$observateur->getObs()->run();
				}
			} catch (Exception $e) {
				die($e->getMessage());
			}
		}
	}
	
	final function attach($event,$status,$ref) {
		$eo = $this->_eventobserver->getEventObserver($event,$status,$ref);
		//echo "::".$eo->getEvent()."::";
		if(in_array($eo->getEvent(),$this->_events)) {
			echo "on attache l'eventobserver<br>";
			$this->_observateur->attach($eo);
			return $this;
		} else {
			//echo "ERREUR! $event inexistant!";
		}
	}
	
	final function detach($event,$status,$ref) {
		$eo = $this->_eventobserver->getEventObserver($event,$status,$ref);
		$this->_observateur->detach($eo);	
		return $this;
	}
	
	final function getIterator() {
		return $this->_observateur;
	}
	
	final public function listObserver() {
		$buffer = '';
		
		foreach ($this->_observateur as $observateur)
		{				
			$buffer .= "\n". $observateur->getEvent() ." => ". $observateur->getObs()->getName();
		}
		return $buffer;
	}
	
}

?>